package com.ql.mangersystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.ql.mangersystem.common.AuthAccess;
import com.ql.mangersystem.constants.CaptchaConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.ql.mangersystem.constants.RedisConstants.REDIS_CAPTCHA;
import static com.ql.mangersystem.constants.RedisConstants.REDIS_CAPTCHA_EXPIRE;

@RequestMapping("/captcha")
@RestController
@Slf4j
@Api("图形验证码")
@CrossOrigin(value = "http://localhost:5173/")
public class CaptchaController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 生成验证码
     */
    @ApiOperation("验证码生成")
    @AuthAccess
    @GetMapping("/getCaptcha")
    public void getCaptcha(HttpServletResponse response) {
        //定义图形验证码的长宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(CaptchaConstants.CAPTCHA_WIDTH, CaptchaConstants.CAPTCHA_HEIGHT);
        //设置返回类型
        System.out.println(lineCaptcha.getCode());
        stringRedisTemplate.opsForValue().set(REDIS_CAPTCHA+lineCaptcha.getCode(),lineCaptcha.getCode(),REDIS_CAPTCHA_EXPIRE, TimeUnit.MINUTES);
        response.setContentType("image/jpeg");
        //禁止使用缓存
        try {
            //输出到页面
            lineCaptcha.write(response.getOutputStream());
            //将生成的验证码和时间存入session中
//            session.setAttribute(CaptchaConstants.CAPTCHA_KEY, lineCaptcha.getCode());
//            String code = (String) session.getAttribute(CaptchaConstants.CAPTCHA_KEY);
//            System.out.println("code为："+code);
//            session.setAttribute(CaptchaConstants.CAPTCHA_DATE, new Date());
            //关闭流
            response.getOutputStream().close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 验证码校验
     */
    @PostMapping("/check")
    @AuthAccess
    public boolean checkCaptcha(String captcha, HttpSession session) {
        System.out.println(captcha);
        log.info("接收到验证码: {}", captcha);
        // 参数校验
        if(!StringUtils.hasLength(captcha)) {
            System.out.println("参数为空");
            return false;
        }
        // 获取存储的验证码和生成时间
        String code = (String) session.getAttribute(CaptchaConstants.CAPTCHA_KEY);
//        System.out.println("code"+code);
        Date createTime = (Date) session.getAttribute(CaptchaConstants.CAPTCHA_DATE);
        // 判断验证码是否正确(验证码一般忽略大小写)
        if(captcha.equalsIgnoreCase(code)) {
            // 判断验证码是否过时
            return createTime == null || System.currentTimeMillis() - createTime.getTime() < CaptchaConstants.EXPIRATION_TIME;
        }
        return false;
    }
}
